﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Warehouse.DataAccess
{
    public partial class PurchaseBill : CustomeEntity<PurchaseBill>
    {
        public PurchaseBillType BillType
        {
            get { return (PurchaseBillType)this.Type; }
            set { this.Type = (int)value; }
        }
        public BillStatus BillStatus
        {
            get { return (BillStatus)this.Status; }
            set { this.Status = (int)value; }
        }

        protected override System.Linq.Expressions.Expression<Func<PurchaseBill, bool>> GetPredicate()
        {
            return p => p.ID == this.ID;
        }

        #region Public Static Methods
        public static PurchaseBill GetOne(Guid Id)
        {
            using (WarehouseClassesDataContext context = new WarehouseClassesDataContext())
            {
                return (from t in context.PurchaseBill where t.ID == Id select t).SingleOrDefault();
            }
        }

        public static PurchaseBill[] Search(DateTime dtStart, DateTime dtEnd, int supplierID, int billType, string serialNumber, string summary)
        {
            using (WarehouseClassesDataContext context = new WarehouseClassesDataContext())
            {
                return (from t in context.PurchaseBill where (supplierID == -1 || t.SupplierID == supplierID) && (billType == -1 || t.Type == billType) && (serialNumber == "" || t.SerialNumber == serialNumber) && (summary == "" || t.Summary.Contains(summary)) && t.PurchaseDate >= dtStart && t.PurchaseDate < dtEnd orderby t.PurchaseDate, t.SerialNumber ascending select t).ToArray();
            }
        }
        #endregion
    }

    public enum PurchaseBillType
    {
        Init = 0,
        Purchase = 1,
        Gift = 2,
        Return = 3
    }
}
